package com.first.nell.utils.imageUtil;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.first.nell.ProjectApplication;
import com.first.nell.ProjectConfig;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageConvert {

    public static final String TAG = "FileChecker";

    public static String getImageDir() {
        String imgDir = ProjectApplication.context.getApplicationContext().getFilesDir() + "/" + ProjectConfig.NARSCASH_Product + "Pics";
        File dirFile = new File(imgDir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        return imgDir;
    }

    public static String getImageFilePath() {
        String pictureId = (System.currentTimeMillis() + "");
        String imgDir = getImageDir();
        return imgDir + "/" + pictureId + ".jpg";
    }

    public static Bitmap getBitmapFromFilePath(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static File getBitmapFile(Bitmap bitmap) throws IOException {
        if (bitmap == null) {
            return null;
        }

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String targetPath = ImageConvert.getImageFilePath();
        File outFile = new File(targetPath);

        FileOutputStream fos = new FileOutputStream(outFile);
        fos.write(stream.toByteArray());
        fos.flush();
        fos.close();
        stream.flush();
        stream.close();

        return outFile;
    }

    public static String getFileExtendSuffix(File file) {
        try {
            FileInputStream input = new FileInputStream(file);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(input, null, options);
            return options.outMimeType.replace("image/", ".");
        } catch (Exception e) {
            return ".jpg";
        }
    }

    public static void startConvertImage(File file, final OnP6wfgc12ckyhiuirzwcsggListener onOnP6wfgc12ckyhiuirzwcsggListener) {
        try {
            if (file != null) {
                String suffix = getFileExtendSuffix(file);
                String targetDir = ImageConvert.getImageDir();
                String targetPath = targetDir + "/" +
                        System.currentTimeMillis() +
                        (TextUtils.isEmpty(suffix) ? ".jpg" : suffix);

                File outFile = new File(targetPath);
                File result = ImageChecker.SINGLE.needCompress(300, file.getAbsolutePath()) ?
                        new ImageCompress(file, outFile, false).compress() : file;

                String localPath = result.getAbsolutePath();
                Bitmap thumpBitmap = getBitmapFromFilePath(localPath);

                if (onOnP6wfgc12ckyhiuirzwcsggListener != null) {
                    onOnP6wfgc12ckyhiuirzwcsggListener.onSuccess(localPath, thumpBitmap);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public interface OnP6wfgc12ckyhiuirzwcsggListener {
        /**
         * 返回转换后的路径与图片
         */
        void onSuccess(String localPath, Bitmap thumpImg);
    }
}
